package com.zbbt.sportslottery.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.google.gson.JsonObject;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.app.ServerConfig;
import com.zbbt.sportslottery.bean.BallDataBean;
import com.zbbt.sportslottery.bean.DrawNotice;
import com.zbbt.sportslottery.bean.SuperResult;
import com.zbbt.sportslottery.net.TypeGsonHttpHandler;
import com.zbbt.sportslottery.net.ZbHttpClient;
import com.zbbt.sportslottery.ui.adapter.DrawHistoryAdapter;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.KeyUtil;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.UmengUtil;

public class DrawHistoryActivity extends SimpleBaseActivity {

	private SwipeRefreshLayout refreshLayout;
	public LayoutInflater inflater;
	private ProgressBarCircularIndetermininate progressBar;
	private ListView mListView;
	private DrawHistoryAdapter adapter;
	private TextView righttext;
	private List<BallDataBean> result = new ArrayList<BallDataBean>();
	private String pageSize = "10";
	private int pageNumber = 1;
	private String titleText = "";
	private String gameId = "";
	private boolean hasMore = false;// 是否有下一页数据
	private String lotterycode;
	private String areaId;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.fragment_super_history);
		Intent intent = getIntent();
		gameId = intent.getStringExtra("type");
		titleText = intent.getStringExtra("name");
		lotterycode = intent.getStringExtra("lotterycode");
		areaId = intent.getStringExtra("areaId");
	}

	@Override
	protected void initView() {
		righttext = (TextView) findViewById(R.id.history_tv_right);
		mListView = (ListView) findViewById(R.id.listview_super_history);
		refreshLayout = (SwipeRefreshLayout) findViewById(R.id.super_history_container);
		progressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.progressBarCircularIndetermininate);
		inflater = LayoutInflater.from(context);
		setTitle(titleText);
		if (titleText.contains("11选5")) {
			righttext.setVisibility(View.VISIBLE);
		}
		if (adapter == null) {
			adapter = new DrawHistoryAdapter(context, result, handler);
			mListView.setAdapter(adapter);
		}
		refreshLayout.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				httpData(Constant.REFRESH);
			}
		});
		progressBar.setVisibility(View.VISIBLE);
		httpData(Constant.REFRESH);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BallDataBean entity = result.get(position);
				// if (entity.getLotteryType() == 2) {// 数字彩跳转
				Intent i = new Intent(context, DrawHisoryDetailActivity.class);
				i.putExtra("name", entity.getLotteryName());
				i.putExtra("date", entity.getResultTime() + "");
				i.putExtra("draw", entity.getDrawId());
				i.putExtra("number", entity.getResult());
				i.putExtra("number2", entity.getAddResult());
				i.putExtra("areaid", entity.getAppId() + "");
				i.putExtra("code", entity.getLotteryCode());
				i.putExtra("id", entity.getLotteryId());
				i.putExtra("type", entity.getLotteryType() + "");
				context.startActivity(i);
				// }
			}
		});
	}

	@Override
	protected void setListener() {
		righttext.setOnClickListener(this);
	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.history_tv_right:
			UmengUtil.onEvent(context, Constant.UMENG_EVLENT_ICON);
			Intent intent = new Intent(DrawHistoryActivity.this, ElevenWebViewActivity.class);
			intent.putExtra("isOpen", Constant.isEvlenIsopen);
			intent.putExtra("evelentUrl", Constant.HREURL);
			intent.putExtra("evelname", Constant.REMARK);
			LogUtil.printD("ccccccccccccc", Constant.isEvlenIsopen + "");
			intent.putExtra(KeyUtil.GotoType, KeyUtil.GOTO_FRAGMENT);
			intent.putExtra(KeyUtil.FragmentKey, KeyUtil.FRAGMENT_ELEVEN);
			startActivity(intent);
			// finish();
			break;
		default:
			break;
		}

	}

	@Override
	protected void initData() {

	}

	/**
	 * 
	 * @MethodName: HttpData
	 * @Description: 连接网络，获得数据
	 * @author: donn
	 * @date: 2014年12月15日 下午7:26:48
	 * @param state
	 *            void
	 */
	protected void httpData(final int state) {
		if (state == Constant.REFRESH) {
			pageNumber = 1;
		}
		JsonObject jsonObject = new JsonObject();
		jsonObject.addProperty("pageNumber", pageNumber);
		jsonObject.addProperty("pageSize", pageSize);
		jsonObject.addProperty("lotteryType", "2");
		jsonObject.addProperty("areaId", areaId);
		jsonObject.addProperty("lotteryCode", lotterycode);// 此参数暂时未用到
		ZbHttpClient.postNewApi(ServerConfig.API_DRAW_HISTORY, jsonObject,
				new TypeGsonHttpHandler<SuperResult<BallDataBean>>(false) {

					@Override
					public void onSuccess(SuperResult<BallDataBean> data) {
						if (data != null) {
							refreshLayout.setRefreshing(false);
							progressBar.setVisibility(View.GONE);
							if (state == Constant.REFRESH) {
								result.clear();
							}
							result.addAll(data.getData());
							// TODO 待接口更新会取消hasMore字段 @王金伦
							if (data.getHasMore() == 0) {
								if (data.getCurPage() != data.getTotalPage()) {
									hasMore = true;
								} else {
									hasMore = false;
								}
							} else if (data.getHasMore() == 1) {
								hasMore = true;
							} else {
								hasMore = false;
							}
							adapter.notifyDataSetChanged();
						}
					}

					@Override
					public void onFailure(String errorCode, String errorMsg) {
						refreshLayout.setRefreshing(false);
						progressBar.setVisibility(View.GONE);
					}
				});
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {

			case Constant.HAS_MORE: {// 加载更多
				if (hasMore) {
					pageNumber++;
					httpData(Constant.HAS_MORE);
				}
				break;
			}
			}
		};
	};

}
